﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MusicExplorer.Internals;
using MusicExplorer.ViewModel;
using MusicExplorer.Wrappers;

namespace MusicExplorer.View
{
    /// <summary>
    /// Logique d'interaction pour TrackList.xaml
    /// </summary>
    public partial class TrackList : UserControl
    {
        public TrackList()
        {
            InitializeComponent();

            trackListView.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(ListView_ScrollChanged));
        }

        private void ListView_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            var scrollViewer = ((ScrollViewer)e.OriginalSource);
            ((TrackListViewModel)DataContext).ScrollViewerState = new ScrollViewerState()
            {
                CurrentScroll = scrollViewer.ContentVerticalOffset,
                HeightArea = scrollViewer.ViewportHeight,
                ScrollableHeight = scrollViewer.ScrollableHeight
            };

            // This when new items are loading (for disable autoscrolling)
            if (e.ExtentHeightChange > 0 && scrollViewer.ScrollableHeight > 0 && e.VerticalOffset > 0)
            {
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.VerticalChange);
            }
        }

        private void ResizeColumns()
        {
            //((GridView)trackListView.View).Columns[1].Width = trackListView.ActualWidth * 0.7;
        }

        private void SetToFavorite(object sender, MouseButtonEventArgs e)
        {
            var wrapper = trackListView.SelectedItem as TrackWrapper;
            wrapper.IsFavorite = !wrapper.IsFavorite;
        }
    }
}
